package cn.edu.ujn.lab3.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.CustomerMapper;
import cn.edu.ujn.lab3.service.CustomerService;
import cn.edu.ujn.lab3.util.Page;

@Service
public class CustomerServiceImpl implements CustomerService {
    
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Page<Customer> findCustomerList(Integer page, Integer rows, String custName, String custSource,
            String custIndustry, String custLevel) {
        // 创建客户对象
        Customer customer = new Customer();
       // 判断客户名称
       if(StringUtils.isNotBlank(custName)){
           customer.setCustName(custName);
       }
       // 判断客户信息来源
       if(StringUtils.isNotBlank(custSource)){
           customer.setCustSource(custSource);
       }
       // 判断客户所属行业
       if(StringUtils.isNotBlank(custIndustry)){
           customer.setCustIndustry(custIndustry);
       }
       // 判断客户级别
       if(StringUtils.isNotBlank(custLevel)){
           customer.setCustLevel(custLevel);
       }
       // 当前页
       customer.setStart((page-1) * rows) ;
       // 每页数
       customer.setRows(rows);
       // 查询客户列表
       List<Customer> customers = customerMapper.selectCustomerList(customer);
       // 查询客户列表总记录数
       Integer count = customerMapper.selectCustomerListCount(customer);
       // 创建Page返回对象
       Page<Customer> result = new Page<>();
       result.setPage(page);
       result.setRows(customers);
       result.setSize(rows);
       result.setTotal(count);
       return result;
    }

    
    @Override
    public int create(Customer customer) {
        return customerMapper.insert(customer);
    }


    @Override
    public Customer getById(Integer id) {
        return customerMapper.selectByPrimaryKey(id);
    }


    @Override
    public int update(Customer customer) {
        customer.setCustCreatetime(new Timestamp(new Date().getTime()));
        return customerMapper.updateByPrimaryKey(customer);
    }


    @Override
    public int delete(Integer id) {
        return customerMapper.deleteByPrimaryKey(id);
    }

    
    
    
}
